import { defineStore,StoreState } from 'pinia'
import { useMenu } from '../layout/LaySidebar/menuOptions'
import router, { constantRouters } from "../router/router";

export default defineStore('userStore', {
    state: () => ({
        token: '',
        vcodeToken: '', // 验证码token
        userInfo: {
            account: '',
            userName: '',
            avatar: '',
        },
        menus: [], //菜单
        prem: [],   //按钮权限
        routes: []  // 格式化后的路由
    }),
    actions: {
        SET_TOKEN(token: string) {
            this.token = token;
        },
        SET_VCODE_TOKEN(token: string) {
            this.vcodeToken = token;
        },
        SET_USER_INFO({ userName, account, avatar }: any) {
            this.userInfo = { userName, account, avatar };
        },
        SET_MENUS(menus: []) {
            this.menus = menus;
        },
        SET_PREM(prem: []) {
            this.prem = prem
        },
        SET_ROUTES(routes: []) {
            this.routes = routes;
        },
        LOGIN_OUT() {
            localStorage.clear();
            this.token = '';
            this.vcodeToken = '';
            this.menus = [];
            this.routes = [];
            this.userInfo = {
                account: '',
                userName: '',
                avatar: '',
            }
            router.replace('/login')
        }
    },
    getters: {
        userInfoGetter: (state) => {
            return state.userInfo
        },
        userMenusGetter: (state) => {
            return useMenu(constantRouters).concat(useMenu(state.routes));
        }
    }
})

